
import {
    Component,
    OnInit,
    ViewEncapsulation,
    AfterViewInit,
    ViewChild,
    ElementRef,
    NgZone,
    OnDestroy,
    Injector,
    Input,
    ViewContainerRef,
    ComponentRef,
    Type,
    ComponentFactory
} from '@angular/core';

import {AbmComponent} from 'angular-baidu-maps';

import {AppComponentBase} from '@shared/app-component-base';

import { NzModalSubject, NzModalService, NzMessageService } from 'ng-zorro-antd';

import { FileUploader } from 'ng2-file-upload';

import { AppConsts } from '@shared/AppConsts';

import {  FileDto } from '@shared/service-proxies/service-proxies';

@Component({selector: 'upload-data', templateUrl: './upload-data.component.html'})
export class UploadDataComponent extends AppComponentBase implements OnInit, OnDestroy {

    uploader: FileUploader = new FileUploader({
        url: AppConsts.remoteServiceBaseUrl + '/File/UploadTempFile',
        isHTML5: true,
        method: 'POST',
        autoUpload:true
    });

    @Input()
    title?:string;

    @Input()
    action: (file: FileDto,host:any) => Promise<any>;

    @Input()
    host:any;

    constructor(injector : Injector,
        private subject: NzModalSubject) {
        super(injector);

        let self = this;

        this.uploader.onAfterAddingFile = (f) => {

            return f;
        };

        this.uploader.onAfterAddingFile = (file) => {
            file.withCredentials = false;
        };

        this.uploader.onBeforeUploadItem=(file)=>{

        };

        this.uploader.onCompleteItem=(f,r,s,h)=>{

            let res = JSON.parse(r);
            console.log(res);
            if (!res.success) {
                this.notify.error('上传文件失败');
                return;
            }

            let file = res.result;

            if (self.action == undefined) {

                self.save();
                return;
            }

            self.action(file,self.host).then(
                () => {
                    self.save();
                }
            );
        };
    }


    save(): void {
        this.subject.destroy('onOk');
    }

    close(): void {

        this.subject.destroy();
    }

    ngOnInit() {
        // this.componentRef =  this.container.createComponent(this.compFactory);
        // this.container.createComponent();
    }

    ngOnDestroy() {
     //   this.componentRef.destroy();
    }

}

